package com.totalitems {
	import com.greensock.TweenMax;
	import com.greensock.plugins.TintPlugin;
	import com.greensock.plugins.TweenPlugin;
	import com.reusables.BoxSprite;
	import com.reusables.Triangle;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	TweenPlugin.activate([TintPlugin]);
	public class TotalItems extends Sprite {
		private var _myFormat : TextFormat;
		private var _myFormat2 : TextFormat;
		private var _txtField : TextField;
		private var _txtField2 : TextField = new TextField();
		private var _myFont : Font;
		private var _numBox : BoxSprite;
		private var _arrowBoxTop : BoxSprite;
		private var _arrowBoxBtm : Sprite;
		private var _triangleUp : Triangle;
		private var _triangleDown : Triangle;
		private var _smBoxTop : MovieClip = new MovieClip;
		private var _smBoxBtm : MovieClip = new MovieClip;
		private var _count : Number = 0;

		public function TotalItems() {
			mainTextField();
			createArrowBoxTop();
			createArrowBoxBtm();
			createNumBox();

			_smBoxTop.addEventListener(MouseEvent.CLICK, onUpClick);
			_smBoxBtm.addEventListener(MouseEvent.CLICK, onDownClick);

			addChild(_smBoxTop);
			addChild(_smBoxBtm);
		}

		private function onUpClick(evt : Event) : void {
			_count++;
			_txtField2.text = _count.toString();
		}

		private function onDownClick(evt : Event) : void {
			if (_txtField2.text == "0") {
				_count++;
			} else {
				_count--;
				_txtField2.text = _count.toString();
			}
		}

		private function createArrowBoxTop() : void {
			_triangleUp = new Triangle();
			_triangleUp.x = 4;
			_triangleUp.y = 2;

			_arrowBoxTop = new BoxSprite(14, 11);
			_arrowBoxTop.x = 46;
			_arrowBoxTop.y = 25;
			_arrowBoxTop.buttonMode = true;
			_arrowBoxTop.graphics.lineStyle(.5, 0x000000, 1);
			_arrowBoxTop.graphics.beginFill(0xffffff, 1);
			_arrowBoxTop.graphics.endFill();

			_arrowBoxTop.addChild(_triangleUp);
			_smBoxTop.addChild(_arrowBoxTop);

			_arrowBoxTop.addEventListener(MouseEvent.MOUSE_OVER, onRollover);
			_arrowBoxTop.addEventListener(MouseEvent.MOUSE_OUT, onRollout);
		}

		private function createArrowBoxBtm() : void {
			_triangleDown = new Triangle();
			_triangleDown.x = 4;
			_triangleDown.y = 9;
			_triangleDown.scaleY *= -1;

			_arrowBoxBtm = new BoxSprite(14, 11);
			_arrowBoxBtm.x = 46;
			_arrowBoxBtm.y = 37;
			_arrowBoxBtm.buttonMode = true;
			_arrowBoxBtm.graphics.lineStyle(.5, 0x000000, 1);
			_arrowBoxBtm.graphics.beginFill(0xffffff, 1);
			_arrowBoxBtm.graphics.endFill();

			_arrowBoxBtm.addChild(_triangleDown);
			_smBoxBtm.addChild(_arrowBoxBtm);

			_arrowBoxBtm.addEventListener(MouseEvent.MOUSE_OVER, onRollover);
			_arrowBoxBtm.addEventListener(MouseEvent.MOUSE_OUT, onRollout);
		}

		private function createNumBox() : void {
			_numBox = new BoxSprite(23, 23);
			_numBox.x = 23;
			_numBox.y = 25;
			_numBox.graphics.lineStyle(1, 0x000000, 1);
			_numBox.graphics.beginFill(0xffffff, 1);
			_numBox.graphics.endFill();
			_numBox.mouseEnabled = false;
			addChild(_numBox);

			_myFont = new Font1();

			_myFormat2 = new TextFormat();
			_myFormat2.font = _myFont.fontName;
			_myFormat2.size = 17;
			_myFormat2.letterSpacing = 1;
			_myFormat2.align = TextFormatAlign.CENTER;

			_txtField2.x = 1;
			_txtField2.y = 0;
			_txtField2.width = 23;
			_txtField2.height = 23;
			_txtField2.selectable = false;
			_txtField2.textColor = 0x000000;
			_txtField2.border = false;
			_txtField2.defaultTextFormat = _myFormat2;
			_txtField2.embedFonts = true;
			_txtField2.antiAliasType = AntiAliasType.NORMAL;
			_txtField2.restrict = "0-9";
			_txtField2.text = _count.toString();
			_txtField2.mouseEnabled = false;
			_numBox.addChild(_txtField2);
		}

		private function mainTextField() : void {
			_myFont = new Font1();

			_myFormat = new TextFormat();
			_myFormat.font = _myFont.fontName;
			_myFormat.size = 15;
			_myFormat.letterSpacing = 1;
			_myFormat.align = TextFormatAlign.LEFT;

			_txtField = new TextField();
			_txtField.x = 23;
			_txtField.y = 0;
			_txtField.width = 120;
			_txtField.height = 20;
			_txtField.selectable = false;
			_txtField.textColor = 0x000000;
			_txtField.border = false;
			_txtField.defaultTextFormat = _myFormat;
			_txtField.embedFonts = true;
			_txtField.antiAliasType = AntiAliasType.NORMAL;
			_txtField.text = "total items";
			addChild(_txtField);
		}

		private function onRollover(evt : Event) : void {
			TweenMax.to(evt.currentTarget, 0.5, {colorMatrixFilter:{colorize:0x000000, amount:0.3}});
		}

		private function onRollout(evt : Event) : void {
			TweenMax.to(evt.currentTarget, 0.5, {colorMatrixFilter:{colorize:0xffffff, amount:1}});
		}
	}
}
